package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringConstructOrderDetailView;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringConstructContentBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringConstructOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.customview.DateViewDialog;
import com.cloudsigns.factorymanager2.engineering.customview.ReadDilalog;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringConstructOrderDetailPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallOrderDetailPresenter;
import com.cloudsigns.factorymanager2.project.activity.AddProductInfomationActivity1;
import com.cloudsigns.factorymanager2.project.adapter.ProductContentAdapter;
import com.cloudsigns.factorymanager2.project.adapter.ProductInformationAdapter;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class ViewEngineeringConstructOrderDetailActivity1 extends Activity implements View.OnClickListener, ItemRemoveListener,IEngineeringConstructOrderDetailView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private EditText edt_project_name, edt_engineering_name, edt_detail_address, edt_completion_date;
    private ListView lv_add_product, lv_add_construction_content;
    private ImageView img_completion_date,img_add_product, img_add_construction_content;
    private ProductInformationAdapter adapter;
    private List<ProductBean> productBeanList = new ArrayList<>();
    private final String TAG = "ViewEngineeringConstructOrderDetailActivity1";
    private EngineeringConstructOrderBean engineeringConstructOrderBean;
    private OrderParentBean orderParentBean;
    private EngineeringConstructOrderDetailPresenter engineeringConstructOrderDetailPresenter;
    private ProductContentAdapter productContentAdapter;// 施工内容
    private List<EngineeringConstructContentBean> engineeringContentBeanList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_view_engineering_construct_order_detail1);

        init();
        Intent mIntent = getIntent();
        if (null != mIntent) {
            String tag = mIntent.getStringExtra(ConstantUtil.KEY_1);
            if (!TextUtils.isEmpty(tag)) {
                if (tag.equals("SendEngineeringOrderActivity")) {
                    engineeringConstructOrderBean = (EngineeringConstructOrderBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);
                } else if (tag.equals("HasBeenReceivedFragment") || tag.equals("BackEngineeringOrderFragment")) {
                    orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);
                    engineeringConstructOrderDetailPresenter = new EngineeringConstructOrderDetailPresenter(this);
                    engineeringConstructOrderDetailPresenter.getEngineeringConstructOrderDetail();
                }
            }
        }
        initData();

    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        edt_project_name = findViewById(R.id.edt_project_name);
        edt_engineering_name = findViewById(R.id.edt_engineering_name);
        edt_detail_address = findViewById(R.id.edt_detail_address);
        edt_completion_date = findViewById(R.id.edt_completion_date);
        img_completion_date=findViewById(R.id.img_completion_date);
        img_add_product = findViewById(R.id.img_add_product);
        img_add_construction_content = findViewById(R.id.img_add_construction_content);

        lv_add_product = findViewById(R.id.lv_add_product);
        lv_add_construction_content = findViewById(R.id.lv_add_construction_content);


        tv_title.setText(getResources().getText(R.string.work_order_information));
        rl_back.setOnClickListener(this);
        img_completion_date.setOnClickListener(this);
        img_add_product.setOnClickListener(this);
        img_add_construction_content.setOnClickListener(this);

        adapter = new ProductInformationAdapter(this, TAG);
        adapter.setRemoveListener(this);
        adapter.setData(productBeanList);
        lv_add_product.setAdapter(adapter);


        productContentAdapter = new ProductContentAdapter(this, TAG);
        productContentAdapter.setRemoveListener(this);
        productContentAdapter.setData(engineeringContentBeanList);
        lv_add_construction_content.setAdapter(productContentAdapter);

    }

    private void initData() {
        if (null != engineeringConstructOrderBean) {
            edt_project_name.setText(engineeringConstructOrderBean.getProjectName());
            edt_engineering_name.setText(engineeringConstructOrderBean.getEngineeringName());
            edt_detail_address.setText(engineeringConstructOrderBean.getEngineeringAddress());
            edt_completion_date.setText(DateUtil.getYyyy_Mm_Dd(engineeringConstructOrderBean.getCompleteDate()));

            if (null != engineeringConstructOrderBean.getProductList()) {
                productBeanList.addAll(engineeringConstructOrderBean.getProductList());
                adapter.setData(productBeanList);
                adapter.notifyDataSetChanged();
            }
        } else if (null != orderParentBean) {
            edt_project_name.setText(orderParentBean.getProjectName());
            edt_engineering_name.setText(orderParentBean.getEngineeringName());
        }
    }

    // 完成日期
    private void showDateDialog() {
        DateViewDialog endDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt_completion_date.setText(time);
                    }
                });
        endDateChooseDialog.setTimePickerGone(true);

        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());

        endDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        endDateChooseDialog.showDateChooseDialog();
    }

    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 完成日期
            case R.id.img_completion_date:
                showDateDialog();
                break;
            // 产品信息
            case R.id.img_add_product:
                mIntent = new Intent(ViewEngineeringConstructOrderDetailActivity1.this, AddProductInfomationActivity1.class);
                mIntent.putExtra("type","engineeringConstruct");
                startActivityForResult(mIntent, ConstantUtil.ADD_PRODUCT_INFOMATION);
                break;
            // 施工内容
//            case R.id.img_add_construction_content:
//                EngineeringConstructContentBean eConstructContentBean = new EngineeringConstructContentBean();
//                eConstructContentBean.setConstructPart("施工内容");
//                engineeringContentBeanList.add(eConstructContentBean);
//                productContentAdapter.setData(engineeringContentBeanList);
//                productContentAdapter.notifyDataSetChanged();
//                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.ADD_PRODUCT_INFOMATION: //添加产品信息
                if (data != null) {
                    ProductBean productBean = (ProductBean) data.getSerializableExtra(ConstantUtil.PRODUCTBEAN);
                    if (productBean != null) {
                        productBeanList.add(productBean);
                        adapter.setData(productBeanList);
                        adapter.notifyDataSetChanged();
                    }


                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public String getOrderId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return null;
    }

    @Override
    public void getEngineeringConstructOrderDetailSuccessed(EngineeringConstructOrderBean engineeringConstructOrderBean) {
        this.engineeringConstructOrderBean = engineeringConstructOrderBean;
        initData();
    }

    @Override
    public void getEngineeringConstructOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void remove(int positon) {  //列表中删除事件
        if (positon >= 0 && positon < productBeanList.size()) {
            productBeanList.remove(positon);
            adapter.setData(productBeanList);
            adapter.notifyDataSetChanged();
        }

        if (positon >= 0 && positon < engineeringContentBeanList.size()) {
            engineeringContentBeanList.remove(positon);
            productContentAdapter.setData(engineeringContentBeanList);
            productContentAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void remove(View view) {

    }
}
